import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  InspectionApi
} from '../../common/apis/inspection';
import {
  SUB_PAGES
} from '../../config/router.config';
import {
  deptSelectStore
} from '../../store/deptSelectStore';
import {
  dicSelectStore
} from '../../store/dicSelectStore';

import {
  inspectionHistroyIndexFilterStore
} from '../../store/inspectionHistroyIndexFilterStore';
const app = getApp()
import {
  getElHeightRpx,
} from "../../utils/dom"
import {
  getCurrentPage,
  toDeptSelectPage,
  toDicSelectPage
} from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    choseCellValue: [],
    bodyHeight: 0,
    pageNum: 1,
    pageSize: 10,
    keywords: '',
    list: [],
    tabsPanelList: [{
      label: '资产类型',
      redNum: 0,
      value: 'assetType'
    }, {
      label: '使用科室',
      redNum: 0,
      value: 'useDept'
    }, {
      label: '巡检单元',
      redNum: 0,
      value: 'inspectionUnit'
    }]
  },
  onCell() {
    if (this.data.activedTab == 'useDept') {
      toDeptSelectPage(getCurrentPage(), 'inspectionHistoryListUseDeptValue', 'selectDept')
    } else if (this.data.activedTab == 'assetType') {
      toDicSelectPage(getCurrentPage(), 'inspectionHistoryListAssetType', 'ast_asset_type')
    } else {
      toDicSelectPage(getCurrentPage(), 'inspectionHistoryListUnit', 'ins_unit_directory')
    }

  },

  onCard(e) {
    wx.navigateTo({
      url: `${SUB_PAGES.Inspection.INSPECTION_HISTORY_INDEX_DETAIL.router}?id=${e.currentTarget.dataset.item.id}`,
    })
  },
  onFilter() {
    wx.navigateTo({
      url: SUB_PAGES.Inspection.INSPECTION_HISTORY_INDEX_FILTER.router
    })
  },
  changeSearch: wx.$_.debounce(function ({
    detail
  }) {
    this.setData({
      keywords: detail.value
    })
    this.dataInit()
  }, 300),
  setChoseValue(tabValue) {

    let choseValue = []
    if (tabValue == 'assetType') {
      this.setListApi(InspectionApi.getInspectionHistoryAssetList)

      this.setAssetsType(this.data.inspectionHistoryListAssetType)
      choseValue = this.data.inspectionHistoryListAssetType
    } else if (tabValue == 'useDept') {
      this.setListApi(InspectionApi.getInspectionHistoryAssetList)

      this.setUseDept(this.data.inspectionHistoryListUseDeptValue)
      choseValue = this.data.inspectionHistoryListUseDeptValue
    } else {
      this.setListApi(InspectionApi.getInspectionHistoryUnitList)
      this.setDirectory(this.data.inspectionHistoryListUnit)
      choseValue = this.data.inspectionHistoryListUnit
    }
    this.setActivedTab(tabValue)

    this.setData({
      choseCellValue: choseValue
    })
  },
  onTabsChange(e) {
    this.setChoseValue(e.detail.value)
    wx.nextTick(() => {
      this.dataInit()
    })
  },
  styleInit() {
    const self = this
    getElHeightRpx('#header', (height) => {
      self.setData({
        bodyHeight: app.globalData.viewHeight - height
      })
    })
  },
  dataInit() {
    const {
      directory,
      inspectionEndTime,
      inspectionStartTime,
      inspectUserIdList,
      keyword,
      result,
      assetsType,
      useDept,
      pageNum,
      pageSize
    } = this.data
    const params = {
      directory,
      inspectionEndTime,
      inspectionStartTime,
      inspectUserIdList,
      keyword,
      result,
      assetsType,
      useDept,
      pageNum,
      pageSize
    }
    this.data.listApi(params, (res) => {
      this.setData({
        list: res.data.rows
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeBindings1 = createStoreBindings(this, {
      store: inspectionHistroyIndexFilterStore,
      fields: ['directory', 'inspectionEndTime', 'inspectionStartTime', 'inspectUserIdList', , 'result', 'assetsType', 'useDept', 'activedTab', 'listApi'],
      actions: ['setAssetsType', 'setDirectory', 'setUseDept', 'setActivedTab', 'setListApi']
    })
    this.storeBindings2 = createStoreBindings(this, {
      store: dicSelectStore,
      fields: ['inspectionHistoryListAssetType', 'inspectionHistoryListUnit'],
    })
    this.storeBindings3 = createStoreBindings(this, {
      store: deptSelectStore,
      fields: ['inspectionHistoryListUseDeptValue'],
    })
    this.storeBindings1.updateStoreBindings()
    this.storeBindings2.updateStoreBindings()
    this.storeBindings3.updateStoreBindings()
    this.setChoseValue(this.data.activedTab || 'assetType')

    this.setData({
      listApi: InspectionApi.getInspectionHistoryAssetList,
    })
    wx.nextTick(() => {
      this.dataInit()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

    this.styleInit()

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings1.destroyStoreBindings()
    this.storeBindings2.destroyStoreBindings()
    this.storeBindings3.destroyStoreBindings()


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})